--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
----some constants.
function widget:GetInfo()
  return {
    name      = "Math Library",
    desc      = "Maths",
    author    = "user, revised by Argh",
    date      = "March 3, 2008",
    license   = "GNU GPL, v2 or later",
    layer     = -1,
    enabled   = true  
  }
end

local e = Spring.Echo
local A = math.abs
local R = math.rad
local C = math.ceil
local F = math.floor
local au = 1.618033989

----------------------------------------
------------------some math functions---



local function normalize(v) 
   local d = math.sqrt(v[1]*v[1]+v[2]*v[2]+v[3]*v[3]) 
   if (d == 0) then 
      d = 1
   end
   v[1] = v[1] / d; v[2] = v[2] / d; v[3] = v[3] / d
   return v
end

local function normcrossprod(v1,v2)
   local out = {}  
   out[1] = v1[2]*v2[3] - v1[3]*v2[2] 
   out[2] = v1[3]*v2[1] - v1[1]*v2[3] 
   out[3] = v1[1]*v2[2] - v1[2]*v2[1] 
   out = normalize(out)
   return out 
end

local function goldp(multiplier)
   local res = au * multiplier   
   return res
end

local function DistPoints3d(v1,v2)
   local x = v2[1] - v1[1]
   local y = v2[2] - v1[2]
   local z = v2[3] - v1[3]
   local dxz = math.sqrt(x*x + z*z)
   local dxyz = math.sqrt(dxz*dxz + y*y)   
   return dxyz
end 

local function DistOrigin3d(v1)
   local r = DistPoints3d(v1,{0,0,0})
   return r
end

local function DistPoints2d(v1,v2)
   local x = v2[1] - v1[1]
   local y = v2[2] - v1[2]
   local r = math.sqrt(x*x + y*y) 
   return r
end

local function DistOrigin2d(v1)
   local r = DistPoints2d(v1,{0,0})
   return r
end

----add them to the math table.

math.gold = goldp  --golden proportion
math.GetPointDist3d = DistPoints3d
math.GetOriginDist3d = DistOrigin3d
math.GetPointDist2d = DistPoints3d
math.GetOriginDist2d = DistOrigin3d

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
